<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**     
 * base class
 * @version		1.0.0 
 * @author			sanshi
 */
include_once(dirname(__FILE__)."/PdoMysql.class.php");
class mysqlSession extends PdoMysql
{
	var $tableName = "session";
	var $sessionID = "session_id";
	var $tableF = array("session_data","expiry","last_time");
	var $dbConfigType = "";
	function mysqlSession()
	{
		//设置session的有效时间
		$this->maxLife = get_cfg_var('session.gc_maxlifetime') ? get_cfg_var('session.gc_maxlifetime') : 1440;
	}
	/**
	 * 打开session
	*/
	function __open($save_path, $session_name)
	{
		$this->PdoMysql();
		return true;
	}
	/**
	 * 关闭session
	*/
	function __close()
	{
		return true;
	}
	/**
	 * 读取session
	*/
	function __read($sessionID)
	{
		//$this->__gc($this->maxLife);
		//设置where条件
		$where = "{$this->tableF[1]} > '".time()."' AND {$this->sessionID}='{$sessionID}'";
		//取得数据
		 $sql="SELECT * FROM {$this->tableName} WHERE ".$where;
		$sessionData = $this->select($sql);
		$sessionData = isset($sessionData[0])?$sessionData[0]:"";
		$curTime = time();
		$expiry = $curTime+$this->maxLife;
		//更新session操作时间
	     $this->update(
			 $this->tableName,
			 array(
				$this->tableF[0]=>isset($sessionData[$this->tableF[0]])?$sessionData[$this->tableF[0]]:'',
			    $this->tableF[1]=>$expiry,
			    $this->tableF[2]=>$curTime
			 ),
			 $where);
		return isset($sessionData[$this->tableF[0]]) ? unserialize($sessionData[$this->tableF[0]]) : false;
	}
	/**
	 * 写入session
	*/
	function __write($sessionID, $sessionData)
	{
		//设置where条件
		$where = "{$this->sessionID}='{$sessionID}'";
		//取得数据
		$sql="SELECT * FROM {$this->tableName} WHERE ".$where;
		$data = $this->select($sql);
		$curTime = time();
		$expiry = $curTime+$this->maxLife;
		if(empty($data))
		{
			//插入数据
			$this->insert(
				$this->tableName,
				array($this->sessionID=>$sessionID,$this->tableF[0]=>serialize($sessionData),$this->tableF[1]=>$expiry,$this->tableF[2]=>$curTime)
							);
		}else{
			//更新数据
			$this->update(
					$this->tableName,
					array($this->tableF[0]=>serialize($sessionData),$this->tableF[1]=>$expiry,$this->tableF[2]=>$curTime),
					$where);
		}
	}
	/**
	 * 注销session
	*/
	function __destroy($sessionID)
	{
		$where = "{$this->sessionID}='{$sessionID}'";
		$del = $this->delete($this->tableName,$where);
		return $del==1 ? true : false;
	}
	/**
	 * 销毁过期session
	*/
	function __gc($maxlifetime)
	{
		$where = "{$this->tableF[1]} <= '".time()."'";
		return $this->delete($this->tableName,$where);
	}
}
?>